package website.lhc.commo.base;

import org.springframework.http.HttpStatus;

import java.util.HashMap;

/**
 * @ProjectName: springboot
 * @Package: website.lhc.commo.base
 * @ClassName: Resp
 * @Author: lhc
 * @Description: 返回基础类
 * @Date: 2020/7/5 下午 10:30
 */
public class Resp extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    private static final String CODE = "code";
    private static final String MESSAGE = "message";
    private static final String RESULT = "data";

    public Resp() {
        put(CODE, HttpStatus.OK.value());
        put(MESSAGE, "success");
    }

    public static Resp ok() {
        return new Resp();
    }

    public static Resp ok(String message) {
        Resp resp = new Resp();
        resp.put(MESSAGE, message);
        return resp;
    }

    public static Resp ok(Object result) {
        Resp resp = new Resp();
        resp.put(RESULT, result);
        return resp;
    }

    public static Resp ok(String key, Object value) {
        Resp resp = new Resp();
        resp.put(key, value);
        return resp;
    }

    public static Resp error(int code, String message) {
        Resp resp = new Resp();
        resp.put(CODE, code);
        resp.put(MESSAGE, message);
        return resp;
    }

    public static Resp error(String message) {
        return error(HttpStatus.INTERNAL_SERVER_ERROR.value(), message);
    }

    public static Resp error() {
        return error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "fail");
    }

    @Override
    public Object put(String key, Object value) {
        return super.put(key, value);
    }
}
